package com.sen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sen.common.exception.ApiAsserts;
import com.sen.common.jwt.JwtUtil;
import com.sen.mapper.UmsUserMapper;
import com.sen.model.dto.LoginDto;
import com.sen.model.vo.ProfileVO;
import com.sen.model.dto.RegisterDto;
import com.sen.model.entity.UmsUser;
import com.sen.service.UmsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.utils.MD5Utils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ergou
 * @since 2021-04-08
 */
@Service
@Log4j
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements UmsUserService {

    @Override
    public UmsUser executeRegister(RegisterDto dto) {
        //查询是否有相同用户名的用户
        LambdaQueryWrapper<UmsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsUser::getUsername,dto.getName()).or().eq(UmsUser::getEmail,dto.getEmail());

        //查找一条对象
        UmsUser umsUser = baseMapper.selectOne(wrapper);
        log.debug(umsUser);
        //数据库中存在
        if (!ObjectUtils.isEmpty(umsUser)){
            ApiAsserts.fail("用户名或邮箱已存在！");
        }

        //链式创建UmsUser
        UmsUser addUser = UmsUser.builder()
                .username(dto.getName())
                .alias(dto.getName())
                .password(MD5Utils.getPwd(dto.getPass()))
                .email(dto.getEmail())
                .createTime(new Date())
                .status(true)
                .build();
        baseMapper.insert(addUser);
        return addUser;
    }

    @Override
    public UmsUser getUserByUsername(String username) {
        LambdaQueryWrapper<UmsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsUser::getUsername,username);
        //查找一条对象
        UmsUser umsUser = baseMapper.selectOne(wrapper);
        //数据库中不存在
        if (ObjectUtils.isEmpty(umsUser)){
            ApiAsserts.fail("用户名或密码错误！");
        }

        return umsUser;
    }

    @Override
    public String executeLogin(LoginDto dto) {

        String token = null;
        //1、根据用户名查出全部信息

        try {
            UmsUser user = getUserByUsername(dto.getUsername());
            //密码加密
            String encodePwd =  MD5Utils.getPwd(dto.getPassword());
            if (!encodePwd.equals(user.getPassword())){

                    throw new Exception("用户名或密码错误！");

            }
            //用户名作为生成token的基础
            token = JwtUtil.generateToken(String.valueOf(user.getUsername()));

        } catch (Exception e) {
            log.warn("用户名不存在or密码验证失败=====》"+e.getMessage());
        }
        return token;
    }

    @Override
    public ProfileVO getUserProfile(String id) {
        ProfileVO profile = new ProfileVO();
        UmsUser user = baseMapper.selectById(id);
        BeanUtils.copyProperties(user, profile);

        return profile;
    }
}
